package com.zfei.gfb.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.zfei.gfb.api.StatsService;
import com.zfei.gfb.api.domain.StatsTag;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbLandResourcesMapper;
import com.zfei.gfb.device.AppContext;
import com.zfei.gfb.model.GfbLandResources;
import com.zfei.gfb.model.GfbUser;
import com.zfei.gfb.query.GfbLandResourcesQuery;
import com.zfei.gfb.request.GfbDelRequest;
import com.zfei.gfb.request.GfbGetRequest;
import com.zfei.gfb.request.GfbLandResourcesAddRequest;
import com.zfei.gfb.request.GfbLandResourcesEditRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Gsons;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.util.gson.Types;
import com.zfei.gfb.vo.GfbLandResourcesListVo;
import com.zfei.gfb.vo.GfbLandResourcesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
/**
 * @version 1.0
 * @author
 */
@Auth(type = {Const.VILLAGE_MANAGER, Const.TOWN_MANAGER})
@Api(tags = "土地，池塘，山林招标管理接口")
@RestController
@RequestMapping("/web/land/resources")
public class GfbLandResourcesController  {

	@Resource
	private GfbLandResourcesMapper gfbLandResourcesMapper;

	@Resource
	private HttpSession httpSession;

	@Resource
	private StatsService statsService;

	//浏览量
	@ApiOperation(value = "列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = GfbLandResourcesListVo.class),
	})
	@LogResult
	@HttpJson("/list")
	public Object list(@RequestBody GfbLandResourcesQuery query) {
		query.build(Now::millis);

		GfbLandResourcesListVo result = new GfbLandResourcesListVo();

		int total = gfbLandResourcesMapper.total(query);
		result.setTotal(total);
		if (total == 0) {
			result.setData(Lists.newArrayList());
			return result;
		}

		List<GfbLandResources> list = gfbLandResourcesMapper.list(query);

		//list image
		List<GfbLandResourcesVo> vos = new ArrayList<>();
		for (GfbLandResources item : list) {
			GfbLandResourcesVo vo = Beans.copy(item, GfbLandResourcesVo.class);
			List<String> images =  Gsons.fromJson(item.getImages(), Types.StringListType);

			vo.setImages(images);
			switch (item.getType()){
				case Const.LAND:
                    vo.setBrowseNum(statsService.get(item.getId().toString(), StatsTag.LAND_RESOURCE_BROWSE_COUNT).getNum());
					break;
				case Const.POOL:
                    vo.setBrowseNum(statsService.get(item.getId().toString(), StatsTag.POND_RESOURCE_BROWSE_COUNT).getNum());
					break;
				case Const.FOREST:
                    vo.setBrowseNum(statsService.get(item.getId().toString(), StatsTag.MOUNTAIN_RESOURCE_BROWSE_COUNT).getNum());
					break;
                default:break;
			}

			vos.add(vo);
		}

		result.setData(vos);
		return result;

	}

	@ApiOperation(value = "新增")
	@LogResult
	@HttpJson("/add")
	public Object add(@RequestBody GfbLandResourcesAddRequest request) {

		request.checkInsertNull();
		GfbLandResources record = Beans.copy(request, GfbLandResources.class);

		GfbUser user = (GfbUser) httpSession.getAttribute(AppContext.SESSION_WEB_USER);

		record.setUserId(user.getId());
		record.setImages(Gsons.toJson(request.getImages()));
		record.setCreateTime(Now.millis());
		record.setUpdateTime(Now.millis());
		record.setCntime(DateUtil.now());

		gfbLandResourcesMapper.insertSelective(record);
		return null;
	}

	@ApiOperation(value = "编辑")
	@LogResult
	@HttpJson("/edit")
	public Object edit(@RequestBody GfbLandResourcesEditRequest request) {

		request.checkUpdateNull();

		GfbLandResources record = gfbLandResourcesMapper.selectByPrimaryKey(request.getId());
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}

		BeanUtils.copyProperties(request, record);
		record.setImages(Gsons.toJson(request.getImages()));
		record.setUpdateTime(Now.millis());

		gfbLandResourcesMapper.updateByPrimaryKeySelective(record);

		return null;
	}

	@ApiOperation(value = "删除")
	@LogResult
	@HttpJson("/del")
	public Object del(@RequestBody GfbDelRequest req) {
		req.checkDelNull();
		Integer id = req.getId();

		GfbLandResources record = gfbLandResourcesMapper.selectByPrimaryKey(id);
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}

		gfbLandResourcesMapper.deleteByPrimaryKey(id);
		return null;
	}

	@ApiOperation(value = "土地，池塘，山林招标详情")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbLandResourcesVo.class),
	})
	@LogResult
	@HttpJson("/get")
	public Object get(@RequestBody GfbGetRequest request) {
		request.checkGetNull();
		Integer id = request.getId();

		GfbLandResources record = gfbLandResourcesMapper.selectByPrimaryKey(id);

		GfbLandResourcesVo vo = Beans.copy(record, GfbLandResourcesVo.class);
		List<String> images =  Gsons.fromJson(record.getImages(), Types.StringListType);
		vo.setImages(images);

		return vo;
	}
}
